import requests
import random
import json
def login(username='17888808946', password='547360705'):
    session = requests.Session()
    user_agents = [
        'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 '
        'Mobile/13B143 Safari/601.1]',
        'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) '
        'Chrome/48.0.2564.23 Mobile Safari/537.36',
        'Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) '
        'Chrome/48.0.2564.23 Mobile Safari/537.36']

    headers = {
        'User_Agent': random.choice(user_agents),
        'Referer': 'https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F',
        'Origin': 'https://passport.weibo.cn',
        'Host': 'passport.weibo.cn'
    }
    post_data = {
        'username': '',
        'password': '',
        'savestate': '1',
        'ec': '0',
        'pagerefer': 'https://passport.weibo.cn/signin/welcome?entry=mweibo&r=http%3A%2F%2Fm.weibo.cn%2F&wm=3349&vt=4',
        'entry': 'mweibo'
    }

    login_url = 'https://passport.weibo.cn/sso/login'
    post_data['username'] = username
    post_data['password'] = password
    r = session.post(login_url, data=post_data, headers=headers)
    data = json.loads(r.text)
    url1 = data['data']['crossdomainlist']['weibo.com']
    url2 = data['data']['crossdomainlist']['sina.com.cn']
    url3 = data['data']['crossdomainlist']['weibo.cn']
    uuid = data['data']['uid']
    session.get(url1)
    session.get(url2)
    session.get(url3)
    url = 'https://weibo.com/u/{}/home'.format(uuid)
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36",
        "Referer": "https://weibo.com/",
    }
    session.get(url, headers=header)
    if r.status_code != 200:
        raise Exception('模拟登陆失败')
    else:
        print("模拟登陆成功,当前登陆账号为：" + username)
        cookies = session.cookies
        return requests.utils.dict_from_cookiejar(cookies)